#include <iostream>
using namespace std;
template<class T>
class Vector{
public:
    Vector(){
        _start=_finish=_end=nullptr;
    }     
    ~Vector(){
        T*temp=_start;
        while(_start!=_finish){
            _alloc.destroy(_start);
            ++_start;
        }
        if(_start!=nullptr){
            _alloc.deallocate(temp,capacity());
        }
    }
    void push_back(const T & value){
        if(size()==capacity()){
            reallocate();
        } 
        *_finish=value;
        ++_finish;
    }      
    void pop_back(){
        if(size()>0){
            _alloc.destroy(--_finish);
        
        }
    }         
    int size(){
        return _finish-_start;
    }
    int capacity(){
        return _end-_start;
    }
    void display(){
        T* temp=_start;
        while(temp!=_finish){
            cout<<*temp<<" ";
            ++temp;
        }
        cout<<endl;
    }
private:
    static std::allocator<T> _alloc;
    void  reallocate(){
        //先拿到旧的size和capacity
        size_t old_cap=capacity();
        size_t old_size=size();
        size_t new_cap=1;
        T* new_start=nullptr;
        T* new_finish=nullptr;
        //第一次扩容
        if(old_size==0){
        new_start=_alloc.allocate(new_cap);
        new_finish=new_start;
        }
        else{
            //其他次扩容
            new_cap=old_cap*2;
            new_start=_alloc.allocate(new_cap);
            new_finish=new_start;
            for(T*p=_start;p<_finish;++p){
                *new_finish=*p;
                ++new_finish;
                _alloc.destroy(p);
            }
            //清空旧空间
            _alloc.deallocate(_start,old_cap);
        }
        //给_start,_finish赋值
        _start=new_start;
        _finish=new_finish;
        _end=_start+new_cap;
    }
                                                                        

private:
    T* _start;
    T* _finish;
    T* _end;
};
template <typename T>
std::allocator<T> Vector<T>::_alloc;
void test(){
    Vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);
    vec.push_back(6);
    vec.push_back(7);
    vec.push_back(8);
    vec.push_back(9);
    vec.push_back(10);
    vec.pop_back();
    vec.display();
}
int main(int argc, char * argv[]){
    test();
    return 0;
}

